home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacGames Sampler
/
PHT MacGames Bundle.iso
/
MacSource Folder
/
Samples from the CD
/
C and C++
/
sesame C ƒ
/
Sort.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-12-11
|
4KB
|
167 lines
/* SIMPLE C TEST PROGRAM to sort a file */
/* This sample program sorts the contents of 1 file into another. */
/* I make the usual apologies about programming style. */
/* It shows how to write a more standard C program that is to say */
/* a non-Macintosh like program. It also shows the use of printf */
/* for output */
/* The setup stuff for a Mac program is invisible. It is invoked by */
/* loading C-ROOT first, which then loads main(). It defines a basic */
/* Mac environment for these straightforward C programs */
#include stdio.h
#define linesize 80
#define linemax linesize-1 /* assume max line size of 80 characters */
main()
/* we don't have multi-dimensional arrays so we fake it with */
/* simple arrays. We declare an array of 10000 and then use */
/* it in clumps of 100, in effect making an array of 100 100 byte groups */
{ int in,volin,out,volout,numline,lptr,lineno[100],curline,sortpos;
char infile[64],line[10000],outfile[64];
volin=getfile(infile);
volout=putfile(infile,"Output filename?",outfile);
openw(infile);
events();
in=0;
out=0;
in=open(infile,volin,1); /* these are some mac compatible open */
/* routines, you can also use fopen */
out=open(outfile,volout,2);
settype(outfile,volout,"TEXT"); /* special mac requirement for file types */
{
lptr=0;
printf("%s","First Read in the File...\n");
while(1) /*read in the file*/
{
if(readln(in,&line[lptr]))break;
lptr=lptr+100;
if(lptr==10000)
{
lptr=lptr-100;
printf("%s","Sorry, can't sort more than 100 lines...\n");
break;
}
}
numline=lptr/100;
lineno[0]=0;
printf("%s","Next Sort It...\n");
for(curline=1;curline<=numline;curline++)
{
sortpos=0;
while(sortpos<curline)
{
if(lower(&line[curline*100],&line[lineno[sortpos]*100]))break;
sortpos=sortpos+1;
}
push(curline,sortpos,lineno);
}
printf("%s","Finally Write It to the Screen and File\n");
for(curline=0;curline<=numline;curline++)
writeln(out,&line[lineno[curline]*100]);
}
printf("%s","All Done!!!\n");
close(in);
close(out);
printf("%s","Press any key to end program");
wait();
closew();
}
readln(unit,line)
int unit;
char line[];
{
int charpos;
char k;
if (unit==0)return 1;
charpos=0;
while((k=getc(unit))>0)
{
if((k=='\n')|(charpos>=linemax))break;
line[charpos++]=k;
}
line[charpos]=NULL; /*append null*/
if(k<=0)return 1;
else return 0;
}
outbyte(unit,c)
int unit;
char c;
{
if(c==0)return 0;
putc(c,unit);
return c;
}
writeln(unit,line)
int unit;
char line[];
{
int k;
k=0;
printf("%-79s %c",line,'\n'); /* yes, we do have prinf and scanf too */
while(outbyte(unit,line[k++]));
putc('\n',unit);
}
outdec(n)
int n;
{
if(n<0)
putchar('-');
else n = -n;
outint(n);
}
outint(n)
int n;
{ int q;
q = n/10;
if(q) outint(q);
putchar('0'-(n-q*10));
}
push(curline,sortpos,lineno)
int curline,sortpos,lineno[];
{
int k;
for(k=curline;k>sortpos;k--)lineno[k]=lineno[k-1];
lineno[sortpos]=curline;
}
lower(str1,str2)
char str1[],str2[];
{
int charpos;
charpos=0;
if(str1[0]==NULL)return 1; /* if null line it is the lowest */
while((str1[charpos]!=NULL)&(str2[charpos]!=NULL))
{
if(str1[charpos]!=str2[charpos])
if(str1[charpos]<str2[charpos])return 1;
else return 0;
charpos++;
}
return 0;
}